package com.lyw.GeoLocationClient.ui;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.utils.StringUtils;

public class NotificationHelper {
    private NotificationManager nm;
    private Context mContext;

    private static NotificationHelper notification;

    public static NotificationHelper getInstance(Context context) {
        if (notification == null) {
            notification = new NotificationHelper(context);
        }
        return notification;
    }

    public NotificationHelper(Context context) {
        mContext = context;
        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void notifyItemOutOfGeofance(int id, String name, String msg) {

        String noti = String.format(mContext.getResources().getString(R.string.notifyItem), name);
        String text = String.format(mContext.getResources().getString(R.string.notifyItemMsg), msg);

        Intent i = new Intent(mContext, MapActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(
                mContext,
                R.string.app_name,
                i,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(mContext)
                .setContentTitle(noti)
                .setContentIntent(contentIntent)
                .setVibrate(new long[500])
                .setOnlyAlertOnce(true)
                .setTicker(noti, null)
                .setSmallIcon(R.drawable.icon_gcoding)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
                        R.drawable.icon_gcoding));
        if (!StringUtils.isNullOrEmpty(msg)) {
            builder.setContentText(text);
        }

        Notification notify = builder.build();
        nm.notify(id,notify);
    }

    public void removeNotify(int id) {
        nm.cancel(id);
    }

    public void notifyOutOfGeofance(int id) {

        String noti = mContext.getResources().getText(R.string.out_of_geofance).toString();

        Intent i = new Intent(mContext, MapActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(
                mContext,
                R.string.app_name,
                i,
                PendingIntent.FLAG_UPDATE_CURRENT);



        Notification.Builder builder = new Notification.Builder(mContext)
                .setContentTitle(noti)
                .setContentIntent(contentIntent)
                .setVibrate(new long[1000])
                .setOngoing(true)
                .setTicker(noti, null)

                .setSmallIcon(R.drawable.icon_gcoding)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
                        R.drawable.icon_gcoding));

        Notification notify = builder.build();
        nm.notify(id, notify);
    }




}
